const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldpwd:'',
    newpwd:'',
    newpwd2:'',

  },
  bindKeypwd1: function (e) {
    let _this = this
    _this.setData({
      oldpwd: e.detail.value
    })
  },
  bindKeypwd2: function (e) {
    let _this = this
    _this.setData({
      newpwd: e.detail.value
    })
  },
  bindKeypwd3: function (e) {
    let _this = this
    _this.setData({
      newpwd2: e.detail.value
    })
  },

  // 提交
  toSure: function (e) {
    console.log(e)
    let _this = this

    var oldpwd = _this.data.oldpwd;
    var newpwd = _this.data.newpwd;
    var newpwd2 =_this.data.newpwd2;


    if (oldpwd == '' || newpwd == '' || newpwd2 == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 1000
      })
    } else if (newpwd != newpwd2) {
      wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
        duration: 1000
      })
    } else if (newpwd === newpwd2){
      wx.showLoading({
        title: '网络请求中......',
      })
      wx.request({
        url: 'https://xcx.yhzip.com/api.php/my/resetpass',
        method: 'POST',
        data: {
          uid: app.globalData.user.id,
          pwd: _this.data.oldpwd,
          password: _this.data.newpwd,
          repetpass: _this.data.newpwd2,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          console.log(res.data);
          if (res.data.error) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000,
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/myself/myself',
                  })
                }, 2000)
              }
            })
          }
        }
      })
    }

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})